package com.qf.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.dao.UserDao;
import com.qf.entity.User;
import com.qf.service.IUserService;
import com.qf.utils.MapCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

@Primary/*代表用这个方案*/
@Service
public class UserDBServiceImpl implements IUserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public int register(User user) {
        //验证用户名是否存在
        //select count(*) from user where username=?
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", user.getUsername());
        int count = userDao.selectCount(queryWrapper);
        if (count > 0) {
            //用户名存在
            return -1;
        }
        //验证email是否存在
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("username", user.getUsername());
        int count2 = userDao.selectCount(queryWrapper2);
        if (count2 > 0) {
            //邮箱存在
            return -2;
        }
        //插入用户
        return userDao.insert(user);
    }

    @Override
    public User login(String username, String password) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        User user = userDao.selectOne(queryWrapper);
        return user;
    }

    /*发送验证码邮件，根据用户名*/
    @Override
    public int sendEmailByUserName(String username) {
        //判断用户名是否存在
        //验证用户名是否存在
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        int count = userDao.selectCount(queryWrapper);
        if (count == 0) {
            //用户名不存在返回-1，无法找回密码
            return -1;
        }
        //
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("username", username);
        User user = userDao.selectOne(queryWrapper2);
        //获取当前用户的邮箱
        String email = user.getEmail();
        //生成一个验证码,4位 10000以下 0~1会等于0但不会等于1
        int code = (int) (Math.random() * 9000 + 1000);

        //将验证码保存起来-Map集合中 username-（code，时间）
        MapCodeUtils.putCode(username, code + "");
        //给当前邮箱发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //将邮件对象1修饰一下，方便设置
        MimeMessageHelper mimeMessageHelper = null;

        //设置邮件相关属性
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            //邮件标题
            mimeMessageHelper.setSubject("五子棋密码找回邮件");
            //邮件发送方
            mimeMessageHelper.setFrom("l1439824079@sina.com");
            //邮件接收方
            mimeMessageHelper.setTo(email);//设置普通接收方
            //其他两种配置
            /*            mimeMessageHelper.setCc();设置抄送方
            /*            mimeMessageHelper.setBCc();设置密送方
            */
            //设置内容
            mimeMessageHelper.setText("找回密码的验证码" + code + "若非本人操作请忽略");
            //参数2设置为true就可以发HTML文件
          /*  mimeMessageHelper.setText("<h1>找回密码的验证码</h1>" +
                    " <img src='https://img1.baidu.com/it/u=3425134310,3842975637&fm=26&fmt=auto'" + code + "若非本人操作请忽略" ,true);*/
            //邮件发送时间
            mimeMessageHelper.setSentDate(new Date());
            //
            // mimeMessageHelper.addAttachment("我的附件",new File("E:\1.jpg"));
            //发送邮件
            javaMailSender.send(mimeMessage);
            return 1;
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return -2;//邮件发送失败
    }

    @Override
    public int updatePassword(String username, String newPassword) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        User user = userDao.selectOne(queryWrapper);
        //重新设置用户密码
        user.setPassword(newPassword);
        //修改
        int result = userDao.updateById(user);
        return result;
    }
//根据用户名修改头像
    @Override
    public int updateHeadr(String username, String header) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        User user = userDao.selectOne(queryWrapper);
        //重新设置用户密码
        user.setHeader(header);
        //修改
        int result = userDao.updateById(user);
        return result;
    }
//根据用户id新增欢乐豆,synchronized 是线程安全的关键字
    @Override
    public synchronized int addHappyBean(Integer uid, Integer happyBean) {
      User user=userDao.selectById(uid);
      //设置欢乐豆
        user.setHappyBean(user.getHappyBean()+happyBean);
        //修改用户
        userDao.updateById(user);
        return user.getHappyBean();
    }
}
